package controller;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.Carnet;
import model.Contact;
/**
 * this class set the list action listener of the list of contact when the user click on a contact
 * we display the detail of the contact selected in the Jtextpane in the main window
 * @author Estelle
 *
 */
public class ActionList implements ListSelectionListener{
	JTextPane person = new JTextPane();
	JButton modify = new JButton();
	JFrame window = new JFrame();
	int id;
	Carnet carnet = new Carnet("");
	JList<String> listgroupe;
	
	public ActionList(JFrame mainwindow, JList<String> listgrp, JTextPane detail, JButton mod){
		person = detail; 
		modify = mod;
		window = mainwindow;
		id = 0;
		FileDoc file = new FileDoc();
		carnet.copy(file.recupCarnet("Estelle"));
		listgroupe = listgrp;
	}

	 @Override
	 public void valueChanged(ListSelectionEvent e) {
		 List<Contact> foundgroup = new ArrayList<Contact>();
		 String name;
		 if(carnet.getGroupContact().contains(listgroupe.getSelectedValue().toString())){
			 foundgroup = carnet.searchContactGroup(listgroupe.getSelectedValue().toString());
			 setListContactGroup(foundgroup);
			 
		 }
		else{
				name = listgroupe.getSelectedValue().toString();
				
		    	person.removeAll();
		    	
		    	for(Contact friend : carnet.getContent()){
					if(name.toLowerCase().equals(friend.getLastName().toLowerCase() + " " + friend.getFirstName().toLowerCase()) ){
						person.setText(setPersonDetail(friend));
						break;
					}
					
				}
		    	modify.repaint();
		}  
	 }  
	 
	public String setPersonDetail(Contact con){
		
	StringBuilder date = new StringBuilder();
	date.append(con.getDate().get(3)).append(":").append(con.getDate().get(4)).append(" on ");
	date.append(con.getDate().get(1)+1).append("/").append(con.getDate().get(2)).append("/").append(con.getDate().get(0));
	
	String buff = 
		""+ con.getLastName()+  " " + con.getFirstName() + 
		"\n" + con.getGroupe() +
		"\n\n\t personal numbers :" + con.getPersonalPhone() +
		"\n\n\t HomeAddress : " + con.getHomeAddress() + 
		"\n\n\t CompanyAddress :" + con.getCompanyAddress()+
		"\n\n\t HomePage :" + con.getHomePage() +
		"\n\n\t email :" + con.getEmail() +
		"\n\n\n added at " + date.toString();
		
			
		return buff;			
	}
	
	public void setListContactGroup(List<Contact> found){
		int i =0;
		
		StringBuilder tmp = new StringBuilder();
		
		String[] contentlist = new String[found.size()];
		
		for(Contact con : found)
        {
			tmp.append(con.getLastName()).append(" ").append(con.getFirstName());
			System.out.println("test action list l 81 : contact " + tmp.toString());
			contentlist[i]=tmp.toString();
	        tmp.setLength(0);
	        i++;
        }
		listgroupe.setListData(contentlist);
		listgroupe.repaint();
		
	}

}  

